package com.tanhua.server.service;

import com.tanhua.domain.db.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @author liuyp
 * @date 2021/02/02
 */
@Service
public class CachingTestService {

    /**
     * Spring框架在缓存数据时
     *  键是：cacheNames::key
     *      key：支持使用Spring的表达式，叫SpEL表达式
     *           #root.methodName， 获取当前方法名称
     *           #参数名， 获取指定名称的参数值
     *
     *      目前的写法，缓存时键是：user::id的值
     *  值是：方法的返回值，序列化存储的
     */
    // @Cacheable(value = "user", key = "#id")
    @Cacheable(cacheNames = "user", key = "#id")
    public User findById(Long id){
        System.out.println("===findById===");
        User user = new User();
        user.setId(id);
        user.setMobile("13800138000");
        user.setPassword("123456");

        return user;
    }

    // @CacheEvict(cacheNames = "user", key = "#id", allEntries = false)
    @CacheEvict(cacheNames = "user", allEntries = true)
    public void deleteUser(Long id){
        System.out.println("deleteUser");
    }
}
